package com.papper.mng.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.papper.common.bean.sys.SysModel;
import com.papper.common.bean.sys.SysResource;
import com.papper.common.bean.sys.SysSystem;
import com.papper.common.bean.sys.SysUser;
import com.papper.common.config.LocalConfig;
import com.papper.common.config.SysCodeHelp;
import com.papper.common.service.sys.SysLimitService;
import com.papper.common.service.sys.SysModelService;
import com.papper.common.service.sys.SysResourceService;
import com.papper.common.service.sys.SysSystemService;
import com.papper.common.service.sys.SysUserService;
import com.papper.common.utils.tool.DateTool;
import com.papper.mng.controller.BaseAction;
import com.papper.mng.domain.DwzResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author:Lajiao
 * @Date:2014年8月21日
 * @Time:下午12:42:13
 * @Description:主页面框架
 */
@Controller
public class MainAction extends BaseAction {
    @Autowired
    SysUserService     sysUserService;
    @Autowired
    SysResourceService sysResourceService;
    @Autowired
    SysSystemService   sysSystemService;
    @Autowired
    SysLimitService    sysLimitService;
    @Autowired
    SysModelService    sysModelService;


    @GetMapping(value = "/index")
    public String index(Model model) {
        SysUser dbLoginUser = getLoginUser();
        model.addAttribute("sysUser", dbLoginUser);
        // 系统信息
        SysSystem sysSystem = sysSystemService.findSysSystem();
        model.addAttribute("sysSystem", sysSystem);
        model.addAttribute("systemLogo", "");
        // 系统模块
        if (sysSystem != null) {
            List<SysModel> sysModels = sysModelService.findByLimit(sysSystem.getId(), dbLoginUser);
            List<SysModel> listSort  = sysModels.stream().sorted(Comparator.comparing(SysModel::getModelSort)).collect(Collectors.toList());
            model.addAttribute("sysModels", listSort);
        }
        model.addAttribute("stsDay", DateTool.formatDate(new Date(), DateTool.TRADITION_PATTERN));
        return render("system/index");
    }

    @GetMapping(value = "/left")
    public String left(Model model, Long mid) {
        SysUser sysUser = getLoginUser();
        model.addAttribute("mid", mid);
        model.addAttribute("sysUser",sysUser);
        // 超级权限
        if (isSuperUser() && mid == 0) {
            return render("system/left");
        }
        // 过滤菜单
        List<SysResource> sysResources = sysResourceService.findByLimit(mid, sysUser);
        List<SysResource> temp         = Lists.newArrayList();
        if (sysResources != null) {
            for (SysResource sysResource : sysResources) {
                // 保留页面信息
                if (mid.intValue() == sysResource.getModelId().intValue() && sysResource.getResType().intValue() == 0) {
                    temp.add(sysResource);
                }
            }
        }
        model.addAttribute("sysResources", sysResourceService.treeChildList(temp));
        return render("system/left");
    }


    @GetMapping(value = "/limit")
    public String limit(Model model) {
        return render("system/limit");
    }

    @GetMapping(value = "/config/flush")
    @ResponseBody
    public DwzResponse configFlush(HttpServletResponse _response, Model model) throws IOException {
        SysCodeHelp.I().initPropertys();
        LocalConfig.I().initPropertys();
        return new DwzResponse("200", "缓存刷新成功！", "closeCurrent");
    }

    @GetMapping(value = "/index/msg")
    @ResponseBody
    public String msg(Model model, HttpServletResponse _response) throws IOException {
        SysUser sysUser = getLoginUser();
        if (sysUser == null) {
            return new JSONObject().toJSONString();
        }
        /**
         * 检索条件
         */
        /**
         * 返回数据
         */
        return new JSONObject().toJSONString();
    }
}
